package com.codedancer.campus.mapper;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.core.BaseMapper;
import com.codedancer.campus.enums.CircleStatus;
import com.codedancer.campus.po.Circle;
import com.codedancer.campus.qo.circle.CirclePageSearchQo;
import java.util.List;

public interface CircleMapper extends BaseMapper<Circle> {

    /**
     * 查询圈子排名前十
     *
     * @param schoolUuid
     * @return
     */
    default List<Circle> top10OrderByFollowCount(String schoolUuid) {
        LambdaQueryWrapper<Circle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Circle::getCreateSchoolUuid, schoolUuid);
        lambdaQueryWrapper.orderByDesc(Circle::getFollowCount);
        lambdaQueryWrapper.eq(Circle::getDisabled, false);
        lambdaQueryWrapper.eq(Circle::getStatus, CircleStatus.PASSED);
        lambdaQueryWrapper.last("limit 10");
        return selectList(lambdaQueryWrapper);
    }

    /**
     * 分页查询圈子(h5端)
     *
     * @param circlePage
     * @param circlePageSearchQo
     * @return
     */
    default IPage<Circle> pageToClient(Page<Circle> circlePage,
        CirclePageSearchQo circlePageSearchQo) {
        LambdaQueryWrapper<Circle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(circlePageSearchQo.getTitle())) {
            lambdaQueryWrapper.like(Circle::getTitle, circlePageSearchQo.getTitle());
        }
        lambdaQueryWrapper.eq(Circle::getDisabled, false);
        lambdaQueryWrapper.eq(Circle::getStatus, CircleStatus.PASSED);
        lambdaQueryWrapper.eq(Circle::getCreateSchoolUuid, circlePageSearchQo.getSchoolUuid());
        lambdaQueryWrapper.orderByDesc(Circle::getCreateTime);
        return selectPage(circlePage, lambdaQueryWrapper);
    }

    /**
     * 分页查询圈子
     *
     * @param circlePage
     * @param circlePageSearchQo
     * @return
     */
    default IPage<Circle> page2Manager(Page<Circle> circlePage,
        CirclePageSearchQo circlePageSearchQo) {
        LambdaQueryWrapper<Circle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(circlePageSearchQo.getTitle())) {
            lambdaQueryWrapper.like(Circle::getTitle, circlePageSearchQo.getTitle());
        }
        if (ObjectUtil.isNotEmpty(circlePageSearchQo.getSchoolUuid())) {
            lambdaQueryWrapper.eq(Circle::getCreateSchoolUuid, circlePageSearchQo.getSchoolUuid());
        }
        if (ObjectUtil.isNotEmpty(circlePageSearchQo.getDisabled())) {
            lambdaQueryWrapper.eq(Circle::getDisabled, circlePageSearchQo.getDisabled());
        }
        if (ObjectUtil.isNotEmpty(circlePageSearchQo.getStatus())) {
            lambdaQueryWrapper.eq(Circle::getStatus, circlePageSearchQo.getStatus());
        }
        lambdaQueryWrapper.orderByDesc(Circle::getCreateTime);
        return selectPage(circlePage, lambdaQueryWrapper);
    }

    /**
     * 查询需要导出的数据
     *
     * @param circlePageSearchQo
     * @return
     */
    default List<Circle> listExportData(CirclePageSearchQo circlePageSearchQo) {
        LambdaQueryWrapper<Circle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(circlePageSearchQo.getTitle())) {
            lambdaQueryWrapper.like(Circle::getTitle, circlePageSearchQo.getTitle());
        }
        if (ObjectUtil.isNotEmpty(circlePageSearchQo.getSchoolUuid())) {
            lambdaQueryWrapper.eq(Circle::getCreateSchoolUuid, circlePageSearchQo.getSchoolUuid());
        }
        if (ObjectUtil.isNotEmpty(circlePageSearchQo.getDisabled())) {
            lambdaQueryWrapper.eq(Circle::getDisabled, circlePageSearchQo.getDisabled());
        }
        if (ObjectUtil.isNotEmpty(circlePageSearchQo.getStatus())) {
            lambdaQueryWrapper.eq(Circle::getStatus, circlePageSearchQo.getStatus());
        }
        lambdaQueryWrapper.orderByDesc(Circle::getCreateTime);
        return selectList(lambdaQueryWrapper);
    }

    /**
     * 判断该学校圈子标题是否存在
     *
     * @param schoolUuid
     * @param title
     * @return
     */
    default boolean existsBySchoolUuidAndName(String schoolUuid, String title) {
        LambdaQueryWrapper<Circle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Circle::getCreateSchoolUuid, schoolUuid);
        lambdaQueryWrapper.eq(Circle::getTitle, title);
        return selectCount(lambdaQueryWrapper) > 0;
    }

    /**
     * 获取某个学校全部圈子
     *
     * @return
     */
    default List<Circle> listBySchoolUuid(String schoolUuid) {
        LambdaQueryWrapper<Circle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Circle::getDisabled, false);
        lambdaQueryWrapper.eq(Circle::getStatus, CircleStatus.PASSED);
        lambdaQueryWrapper.eq(Circle::getCreateSchoolUuid, schoolUuid);
        return selectList(lambdaQueryWrapper);
    }

    /**
     * 分页查询用户关注或创建的圈子
     *
     * @param circlePage
     * @param circlePageSearchQo
     * @return
     */
    IPage<Circle> page2User(Page<Circle> circlePage, CirclePageSearchQo circlePageSearchQo);

    /**
     * 查询用户创建的圈子
     *
     * @param circlePage
     * @param circlePageSearchQo
     * @return
     */
    default IPage<Circle> pageCreate(Page<Circle> circlePage,
        CirclePageSearchQo circlePageSearchQo) {
        LambdaQueryWrapper<Circle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Circle::getCreateUserUuid, circlePageSearchQo.getCurrentUserUuid());
        lambdaQueryWrapper.orderByDesc(Circle::getCreateTime);
        return selectPage(circlePage, lambdaQueryWrapper);
    }
}
